package scrum.client.project;

import ilarkesto.gwt.client.ADropdownViewEditWidget;
import ilarkesto.gwt.client.AFieldValueWidget;
import ilarkesto.gwt.client.AMultiSelectionViewEditWidget;
import ilarkesto.gwt.client.TableBuilder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import scrum.client.ScrumGwt;
import scrum.client.common.AScrumWidget;

import com.google.gwt.user.client.ui.Widget;

public class ScenarioWidget extends AScrumWidget {

	private Scenario scenario;

	public ScenarioWidget(Scenario scenario) {
		super();
		this.scenario = scenario;
	}

	@Override
	protected Widget onInitialization() {
		TableBuilder tb = ScrumGwt.createFieldTable();

		tb.addFieldRow("Label", scenario.getLabelModel());
		tb.addFieldRow("Criticism", new ADropdownViewEditWidget() {

			@Override
			protected void onViewerUpdate() {
				setViewerItem(scenario.getCriticism());
			}

			@Override
			protected void onEditorUpdate() {
				setOptions(Scenario.CRITICISM_VALUES);
				String option = scenario.getCriticism();
				setSelectedOption(option == null ? "" : option);
			}

			@Override
			protected void onEditorSubmit() {
				String id = getSelectedOption();
				String[] aux = Scenario.CRITICISM_VALUES;
				int index = 0;
				for (int i = 0; i < aux.length; i++) {
					if (aux[i].equals(id)) index = i;
				}
				scenario.setCriticism(id);
				scenario.setCriticismValue(Scenario.CRITICISM_INT_VALUES[index]);
			}

			@Override
			public boolean isEditable() {
				return scenario.isEditable();
			}

			@Override
			public String getTooltip() {
				return "Criticism, what kind of risk you think means that scenario.";
			}
		});
		// -------------------------------------------------------------------------------------------------------------
		tb.addFieldRow("Points", new AFieldValueWidget() {

			@Override
			protected void onUpdate() {
				float aux = 0;
				for (Requirement r : scenario.getStoriesAssociates()) {
					if (r.getEstimatedWork() != null) aux += r.getEstimatedWork();
				}
				setText(aux);
			}
		});
		tb.addFieldRow("Remaining", new AFieldValueWidget() {

			@Override
			protected void onUpdate() {
				float aux = 0;
				for (Requirement r : scenario.getStoriesAssociates()) {
					aux += r.getRemainingWork();
				}
				scenario.setRemaining(aux);
				setText(aux);
			}
		});
		tb.addFieldRow("Priority", new ADropdownViewEditWidget() {

			@Override
			protected void onEditorUpdate() {
				setOptions(Scenario.PRIORITY_VALUES);
				String option = String.valueOf(scenario.getPriority());
				setSelectedOption(option == null ? "1" : option);
			}

			@Override
			protected void onViewerUpdate() {
				setViewerItem(scenario.getPriority());
			}

			@Override
			protected void onEditorSubmit() {
				String id = getSelectedOption();
				scenario.setPriority(Integer.parseInt(id));
			}

			@Override
			public boolean isEditable() {
				return scenario.isEditable();
			}
		});
		tb.addFieldRow("State", new ADropdownViewEditWidget() {

			@Override
			protected void onViewerUpdate() {
				setViewerItem(scenario.getState());
			}

			@Override
			protected void onEditorUpdate() {
				setOptions(Scenario.PROJECT_STATE_VALUES);
				String option = scenario.getState();
				setSelectedOption(option == null ? "" : option);
			}

			@Override
			protected void onEditorSubmit() {
				String id = getSelectedOption();
				scenario.setState(id == null ? "" : id);
			}

			@Override
			public boolean isEditable() {
				return scenario.isEditable();
			}

			@Override
			public String getTooltip() {
				return "State that you think the Scenario is.";
			}
		});
		tb.addFieldRow("Stimulus", scenario.getStimulusModel());
		tb.addFieldRow("Source of Stimulus", scenario.getSourceOfStimulusModel());
		tb.addFieldRow("Artifact Stimulated", scenario.getArtifactStimulatedModel());
		tb.addFieldRow("Environment", scenario.getEnvironmentModel());
		tb.addFieldRow("Response", scenario.getResponseModel());
		tb.addFieldRow("Response Measure", scenario.getResponseMeasureModel());

		tb.addFieldRow("Qualities", new AMultiSelectionViewEditWidget<Quality>() {

			@Override
			protected void onViewerUpdate() {
				List<Quality> qualitys = new ArrayList<Quality>(scenario.getQualitys());
				Collections.sort(qualitys);
				setViewerItemsAsHtml(qualitys);
			}

			@Override
			protected void onEditorUpdate() {
				List<Quality> qualitys = new ArrayList<Quality>(scenario.getProject().getQualitys());
				Collections.sort(qualitys);
				setEditorItems(qualitys);
				setEditorSelectedItems(scenario.getQualitys());
			}

			@Override
			protected void onEditorSubmit() {
				scenario.setQualitys(getEditorSelectedItems());
			}

			@Override
			public boolean isEditable() {
				return scenario.isEditable();
			}
		});

		tb.addFieldRow("Stories", new AMultiSelectionViewEditWidget<Requirement>() {

			@Override
			protected void onViewerUpdate() {
				List<Requirement> requirements = new ArrayList<Requirement>(scenario.getStoriesAssociates());
				Collections.sort(requirements);
				setViewerItemsAsHtml(requirements);
			}

			@Override
			protected void onEditorUpdate() {
				List<Requirement> requirements = new ArrayList<Requirement>(scenario.getProject().getRequirements());
				Collections.sort(requirements);
				setEditorItems(requirements);
				setEditorSelectedItems(scenario.getStoriesAssociates());
			}

			@Override
			protected void onEditorSubmit() {
				scenario.setStoriesAssociates(getEditorSelectedItems());
				List<Requirement> auxRequirements = new ArrayList<Requirement>(getEditorSelectedItems());
				for (Requirement r : auxRequirements) {
					List<Scenario> relatedScenarios;
					if (r.getRelatedScenarios() == null) {
						relatedScenarios = new ArrayList<Scenario>();
						relatedScenarios.add(scenario);
					} else {
						relatedScenarios = new ArrayList<Scenario>(r.getRelatedScenarios());
						relatedScenarios.add(scenario);
					}
					r.setRelatedScenarios(relatedScenarios);
				}
			}

			@Override
			public boolean isEditable() {
				return scenario.isEditable();
			}
		});

		return TableBuilder.row(20, tb.createTable(), ScrumGwt.createEmoticonsAndComments(scenario));
	}

}
